package com.touchapk.javafx;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.math.BigDecimal;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.ImageCursor;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;

/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月31日
 * @version V1.0
 */
public class SelectColorController implements DialogController {

	private MainController mainController;

	public SelectColorController() {
	}

	public SelectColorController(MainController mainController) {
		this.mainController = mainController;
	}

	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}

	@FXML
	private NumberSpinner rNumberSpinner;
	@FXML
	private NumberSpinner gNumberSpinner;
	@FXML
	private NumberSpinner bNumberSpinner;

	@FXML
	private Pane pane;

	@FXML
	private ImageView imageView;

	@FXML
	private TextField rgb;

	@FXML
	private TextField bgr;

	boolean isPressed = false;

	Robot robot;

	double x,y;
	
	@FXML
	void initialize() {
		
		rNumberSpinner.setNumber(new BigDecimal(255));
		gNumberSpinner.setNumber(new BigDecimal(255));
		bNumberSpinner.setNumber(new BigDecimal(255));
		
		try {
			robot = new Robot();
		} catch (AWTException e) {

			e.printStackTrace();
		}

		Image img = new Image(Main.class.getResource("dropper.png")
				.toExternalForm());

		imageView.setImage(img);

		x= img.getWidth() / 2;
		y= img.getHeight() / 2;
		
		imageView.setCursor(new ImageCursor(img,x,y));

		imageView.setOnMousePressed(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				imageView.setVisible(false);

			}
		});
		imageView.setOnMouseReleased(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				imageView.setVisible(true);

			}
		});

		imageView.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {

				Color c = pickColor();
			
				String b=Integer.toHexString(c.getBlue());
				b=b.length()==1?"0"+b:b;
				String g=Integer.toHexString(c.getGreen());
				g=g.length()==1?"0"+g:g;
				String r=Integer.toHexString(c.getRed());
				r=r.length()==1?"0"+r:r;
				
				
				pane.setStyle("-fx-border-width: 1;-fx-border-color: #cccccc;-fx-background-color:#"+(r+g+b).toUpperCase());
				
				rgb.setText("#"+(r+g+b).toUpperCase());
			
				bgr.setText("#"+(b+g+r).toUpperCase());
				
				rNumberSpinner.setNumber(new BigDecimal(c.getRed()));
				gNumberSpinner.setNumber(new BigDecimal(c.getGreen()));
				bNumberSpinner.setNumber(new BigDecimal(c.getBlue()));
				
			}
		});

		rNumberSpinner.numberProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue,
					Object newValue) {
				changeColor();
			}
		});
		
		gNumberSpinner.numberProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue,
					Object newValue) {
			 
				changeColor();
			}
		});
		
		bNumberSpinner.numberProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue,Object newValue) {
				changeColor();
			}
		});
		
	}
	
	public void changeColor(){
		
		int bn=bNumberSpinner.getNumber().intValue();
		bn=bn>255?255:bn<0?0:bn;
		String b=Integer.toHexString(bn);
		b=b.length()==1?"0"+b:b;
		
		int gn=gNumberSpinner.getNumber().intValue();
		gn=gn>255?255:gn<0?0:gn;
		String g=Integer.toHexString(gn);
		g=g.length()==1?"0"+g:g;
		
		int rn=rNumberSpinner.getNumber().intValue();
		rn=rn>255?255:rn<0?0:rn;
		String r=Integer.toHexString(rn);
		r=r.length()==1?"0"+r:r;
		
		
		
		pane.setStyle("-fx-border-width: 1;-fx-border-color: #cccccc;-fx-background-color:#"+(r+g+b).toUpperCase());
		
		rgb.setText("#"+(r+g+b).toUpperCase());
	
		bgr.setText("#"+(b+g+r).toUpperCase());
		
	}
	
	
	

	public Color pickColor() {
		Point mousepoint = MouseInfo.getPointerInfo().getLocation();
		Color pixel = robot.getPixelColor(mousepoint.x-(int)x, mousepoint.y+(int)y);
		return pixel;
	}

}
